package com.zzh.wechatpay.utils;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 封装http get post
 */
public class HttpUtils {


    private static final Gson gson = new Gson();
    private static final Integer SUCCESS_CODE = 200;

    /**
     * get方法
     *
     * @param url 请求地址
     * @return
     */
    public static Map<String, Object> doGet(String url) {

        Map<String, Object> map = new HashMap<>();
        //请求配置,设置连接时间等
        RequestConfig requestConfig = RequestConfig.custom()
                //连接超时
                .setConnectTimeout(5000)
                //请求超时
                .setConnectionRequestTimeout(5000)
                .setSocketTimeout(5000)
                //允许自动重定向
                .setRedirectsEnabled(true)
                .build();
        //get对象
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String jsonResult = EntityUtils.toString(httpResponse.getEntity());
                map = gson.fromJson(jsonResult, map.getClass());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }



    /**
     * 封装post
     * @param url 请求地址
     * @param data 请求参数
     * @param timeout 超时时间
     * @return
     */
//    public static String doPost(String url, String data, int timeout) {
//        //超时设置
//        RequestConfig requestConfig = RequestConfig.custom()
//                .setConnectTimeout(timeout)
//                .setConnectionRequestTimeout(timeout)
//                .setSocketTimeout(timeout)
//                .setRedirectsEnabled(true)
//                .build();
//
//        //post对象
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.setConfig(requestConfig);
//        //设置post请求头部
//        httpPost.addHeader("Content-Type", "text/html; chartset=UTF-8");
//        //使用字符串传参
//        if (data != null) {
//            //参数实体
//            StringEntity stringEntity = new StringEntity(data, "UTF-8");
//            httpPost.setEntity(stringEntity);
//        }
//
//        try (CloseableHttpClient httpClient = HttpClients.createDefault()){
//            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
//            //获取响应的实体对象
//            HttpEntity httpEntity = httpResponse.getEntity();
//            //状态码为200
//            if (httpResponse.getStatusLine().getStatusCode() == SUCCESS_CODE) {
//                return EntityUtils.toString(httpEntity);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//
//    }
    public static String doPost(String url, String data,int timeout){
        CloseableHttpClient httpClient =  HttpClients.createDefault();
        //超时设置

        RequestConfig requestConfig =  RequestConfig.custom().setConnectTimeout(timeout) //连接超时
                .setConnectionRequestTimeout(timeout)//请求超时
                .setSocketTimeout(timeout)
                .setRedirectsEnabled(true)  //允许自动重定向
                .build();


        HttpPost httpPost  = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.addHeader("Content-Type","text/html; chartset=UTF-8");

        if(data != null && data instanceof  String){ //使用字符串传参
            StringEntity stringEntity = new StringEntity(data,"UTF-8");
            httpPost.setEntity(stringEntity);
        }

        try{

            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            if(httpResponse.getStatusLine().getStatusCode() == 200){
                String result = EntityUtils.toString(httpEntity);
                return result;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                httpClient.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return null;

    }

}
